<template>
	<el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" draggable>
		<el-form ref="dataFormRef" :model="dataForm" :rules="dataRules" :label-width="100">
			<el-form-item label="订单号" prop="orderId">
				<el-input v-model="dataForm.orderId" placeholder="订单号"></el-input>
			</el-form-item>
			<el-form-item label="设备编号" prop="deviceId">
				<el-input v-model="dataForm.deviceId" placeholder="设备编号"></el-input>
			</el-form-item>
			<el-form-item label="设备名称" prop="deviceName">
				<el-input v-model="dataForm.deviceName" placeholder="设备名称"></el-input>
			</el-form-item>
			<el-form-item label="订单状态" prop="orderStatus">
				<el-select v-model="dataForm.orderStatus" placeholder="请选择订单状态" :default-first-option="true">
					<el-option v-for="item in orderStatusOptions" :key="item.value" :label="item.label"
						:value="item.value"></el-option>
				</el-select>
			</el-form-item>

			<el-form-item label="开始时间" prop="orderStartTime">
				<el-date-picker v-model="dataForm.orderStartTime" type="datetime" placeholder="选择开始时间"
					value-format="YYYY-MM-DD HH:mm:ss" clearable></el-date-picker>
			</el-form-item>
			<el-form-item label="结束时间" prop="orderEndTime">
				<el-date-picker v-model="dataForm.orderEndTime" type="datetime" placeholder="选择结束时间"
					value-format="YYYY-MM-DD HH:mm:ss" clearable></el-date-picker>
			</el-form-item>
			<el-form-item label="订单总金额" prop="orderTotalCost">
				<el-input v-model="dataForm.orderTotalCost" placeholder="订单总金额（元）" type="number"></el-input>
			</el-form-item>
			<el-form-item label="实付金额" prop="orderRealCost">
				<el-input v-model="dataForm.orderRealCost" placeholder="订单实付金额（元）" type="number"></el-input>
			</el-form-item>
			<el-form-item label="退款金额" prop="orderRefundCost">
				<el-input v-model="dataForm.orderRefundCost" placeholder="订单退款金额（元）" type="number"></el-input>
			</el-form-item>
			<el-form-item label="调度费订单" prop="orderDispatchOrder">
				<el-input v-model="dataForm.orderDispatchOrder" placeholder="调度费订单"></el-input>
			</el-form-item>
			<el-form-item label="支付状态" prop="orderPayStauts">
				<el-select v-model="dataForm.orderPayStatus" placeholder="请选择支付状态" :default-first-option="true">
					<el-option v-for="item in orderPayStatusOptions" :key="item.value" :label="item.label"
						:value="item.value"></el-option>
				</el-select>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取消</el-button>
			<el-button type="primary" @click="submitHandle()">确定</el-button>
		</template>
	</el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus/es'
import { useOrderOrderApi, useOrderOrderSubmitApi } from '@/api/order/order'

const emit = defineEmits(['refreshDataList'])

const visible = defineModel<boolean>('visible')
const dataFormRef = ref()

const dataForm = reactive({
	id: '',
	orderId: '',
	deviceId: '',
	deviceName: '',
	wxOpenId: '',
	orderStatus: '',
	orderStartTime: '',
	orderEndTime: '',
	orderTotalCost: '',
	orderRealCost: '',
	orderRefundCost: '',
	orderDispatchOrder: '',
	orderPayStatus: '',
	creator: '',
	createTime: '',
	updater: '',
	updateTime: '',
	deleted: ''
})

const init = (id?: number) => {
	if (id) {
		getOrder(id)
	}
}

const getOrder = (id: number) => {
    useOrderOrderApi(id).then(res => {
        // 确保状态值正确
        const data = { ...res.data }
        data.orderStatus = String(data.orderStatus || '0')
        data.orderPayStatus = String(data.orderPayStatus || '0')
        
        Object.assign(dataForm, data)
    })
}

const dataRules = ref({
	orderId: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	deviceId: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	orderStatus: [{ required: true, message: '请选择订单状态', trigger: 'change' }],
	orderPayStatus: [{ required: true, message: '请选择支付状态', trigger: 'change' }]
})


const orderStatusOptions = [
	{ value: '0', label: '待支付' },
	{ value: '1', label: '已完成' },
]
const orderPayStatusOptions = [
	{ value: '0', label: '未支付' },
	{ value: '1', label: '已支付' },
]

// 获取默认值对应的 label
const getOrderStatusLabel = (value: string) => {
	const option = orderStatusOptions.find(item => item.value === value)
	return option ? option.label : ''
}
const getOrderPayStatusLabel = (value: string) => {
	const option = orderPayStatusOptions.find(item => item.value === value)
	return option ? option.label : ''
}


// 表单提交
const submitHandle = () => {
	dataFormRef.value.validate(async (valid: boolean) => {
		if (!valid) {
			return false
		}


		useOrderOrderSubmitApi(dataForm).then(() => {
			ElMessage.success({
				message: '操作成功',
				duration: 500,
				onClose: () => {
					visible.value = false
					emit('refreshDataList')
				}
			})
		})
	})
}

defineExpose({
	init
})
</script>
